// pages/yqh/my/index.js
const app = getApp(); // 获取应用实例

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bomMenus: [], //底部菜单数据
    bomMenuId: 5, //页面ID
    // 用户信息
    wxname: '微信昵称', wxhead: '/imgs/goods.png',
    shoplist: [], isshop: 0,
    wxshoptype: 0, wxshopid: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.bomMenuList();
    this.myInfo();
    this.getTmpList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 页面跳转
  toView: function(e) {
    console.log(e);
    var id = e.currentTarget.dataset.id;
    if (id == this.data.bomMenuId) { return }
    var bms = this.data.bomMenus, mlink = '', i = 0;
    for (; i < bms.length; i ++) {
      if (bms[i].id == id) {
        mlink = bms[i].link;
      }
    }
    console.log(mlink);
    wx.navigateTo({
      url: mlink,
    });
  },

  // 底部菜单
  bomMenuList: function () {
    var _this = this;
    wx.request({
      method: 'post',
      url: app.globalData.xcxApi + 'init',
      data: { type: 2 },
      header: { 'Content-Type': 'application/json' },
      success: function (res) {
        console.log(res.data);
        res = res.data;
        if (res.code < 0) {
          return
        }
        _this.setData({
          bomMenus: res.data,
        });
      },
      error: function (err) {
        console.log(err);
      }
    });
  },

  // 我的页面
  myInfo: function (e) {
    var uinfo = wx.getStorageSync('uinfo');
    console.log(uinfo);
    if (!uinfo) {
      wx.showToast({
        title: '没有登录', icon: 'none'
      }); return
    }
    this.setData({
      wxname: uinfo.nick,
      wxhead: uinfo.head,
      wxshoptype: uinfo.shop_type,
      wxshopid: uinfo.shop_id,
    });
  },

  // 获取模板集合
  getTmpList: function (e) {
    var uinfo = wx.getStorageSync('uinfo');
    var _this = this;
    wx.request({
      method: 'post',
      url: app.globalData.xcxApi + 'type/init',
      data: {},
      header: { 'Content-Type': 'application/json' },
      success: function (res) {
        console.log(res.data);
        res = res.data;
        if (res.code < 0) {
          return
        }
        _this.setData({
          isshop: res.data.sel,
          shoplist: res.data.type,
          wxshoptype: uinfo.shop_type,
          wxshopid: uinfo.shop_id,
        });
      },
      error: function (err) {
        console.log(err);
      }
    });
  },

  // 设置门店
  toShop: function (e) {
    console.log(e);
    var type = e.currentTarget.dataset.type;
    var id = e.currentTarget.dataset.id;
    // console.log(type, id); return
    var url = '';
    if (type == 1) {
      url = 'mall';
    } else if (type == 2) {
      url = 'yqh';
    } else {
      wx.showToast({
        title: '地址错误', icon: 'none'
      }); return
    }
    var uinfo = wx.getStorageSync('uinfo');
    uinfo.shop_type = type;
    uinfo.shop_id = id;
    wx.setStorageSync('uinfo', uinfo);
    var pa = '/pages/' + url + '/index/index';
    pa += '?type=' + type + '&shopid=' + id;
    wx.navigateTo({
      url: pa,
    });
  },

  // // 回到首页
  // toback: function (e) {
  //   wx.navigateBack({
  //     url: '/pages/yqh/index/index',
  //   });
  // },
})